#region File Description
//-----------------------------------------------------------------------------
// InputState.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
#endregion

namespace Attack_of_the_Crazy_Pixels
{
    /// <summary>
    /// Helper for reading input from keyboard and mouse. This class tracks both
    /// the current and previous state of both input devices, and implements query
    /// methods for high level input actions such as "move up through the menu"
    /// or "pause the game".
    /// </summary>
    public class InputState
    {
        #region Fields

        public KeyboardState CurrentKeyboardState { get; private set; }
        public MouseState CurrentMouseState { get; private set; }
        public Vector2 CurrentMousePosition { get; private set; }

        public KeyboardState LastKeyboardState { get; private set; }
        public MouseState LastMouseState { get; private set; }
        public Vector2 LastMousePosition { get; private set; }
        

        #endregion

        #region Initialization


        /// <summary>
        /// Constructs a new input state.
        /// </summary>
        public InputState()
        {
            CurrentKeyboardState = new KeyboardState();
            CurrentMouseState = new MouseState();
            CurrentMousePosition = Vector2.Zero;

            LastKeyboardState = new KeyboardState();
            LastMouseState = new MouseState();
            LastMousePosition = Vector2.Zero;
        }


        #endregion

        #region Public Methods


        /// <summary>
        /// Reads the latest state of the keyboard and mouse.
        /// </summary>
        public void Update()
        {
            LastKeyboardState = CurrentKeyboardState;
            LastMouseState = CurrentMouseState;
            LastMousePosition = CurrentMousePosition;

            CurrentKeyboardState = Keyboard.GetState();
            CurrentMouseState = Mouse.GetState();
            CurrentMousePosition = new Vector2((float)CurrentMouseState.X, (float)CurrentMouseState.Y);
        }


        /// <summary>
        /// Helper for checking if a key was newly pressed during this update.
        /// </summary>
        public bool IsNewKeyPress(Keys key)
        {
            return (CurrentKeyboardState.IsKeyDown(key) && LastKeyboardState.IsKeyUp(key));
        }


        /// <summary>
        /// Helper for checking if a mouse button was newly pressed during this update.
        /// </summary>
        public bool IsNewButtonPress(MouseButtons button)
        {
            switch (button)
            {
                case MouseButtons.LeftButton:
                    return (CurrentMouseState.LeftButton == ButtonState.Pressed && LastMouseState.LeftButton == ButtonState.Released);
                case MouseButtons.RightButton:
                    return (CurrentMouseState.RightButton == ButtonState.Pressed && LastMouseState.RightButton == ButtonState.Released);
                case MouseButtons.MiddleButton:
                    return (CurrentMouseState.MiddleButton == ButtonState.Pressed && LastMouseState.MiddleButton == ButtonState.Released);
                default:
                    return false;
            }
        }


        /// <summary>
        /// Checks for a "menu select" input action.
        /// </summary>
        public bool IsMenuSelect()
        {
            return (IsNewKeyPress(Keys.Space) || IsNewKeyPress(Keys.Enter));
        }


        /// <summary>
        /// Checks for a "menu cancel" input action.
        /// </summary>
        public bool IsMenuCancel()
        {
            return IsNewKeyPress(Keys.Escape);
        }


        /// <summary>
        /// Checks for a "menu up" input action.
        /// </summary>
        public bool IsMenuUp()
        {
            return IsNewKeyPress(Keys.Up);
        }


        /// <summary>
        /// Checks for a "menu down" input action.
        /// </summary>
        public bool IsMenuDown()
        {
            return IsNewKeyPress(Keys.Down);
        }

        /// <summary>
        /// Checks for a "menu left" input action.
        /// </summary>
        public bool IsMenuLeft()
        {
            return IsNewKeyPress(Keys.Left);
        }

        /// <summary>
        /// Checks for a "menu right" input action.
        /// </summary>
        public bool IsMenuRight()
        {
            return IsNewKeyPress(Keys.Right);
        }


        /// <summary>
        /// Checks for a "pause the game" input action.
        /// </summary>
        public bool IsPauseGame()
        {
            return IsNewKeyPress(Keys.Escape);
        }


        #endregion
    }

    public enum MouseButtons
    {
        LeftButton,
        RightButton,
        MiddleButton
    }
}
